package com.unohacha.qqj.fzdr.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.bean.LabelBean;
import com.unohacha.qqj.fzdr.bean.find.FindDetailBean;

import java.util.ArrayList;
import java.util.List;

public class LabelCategaryAdapter extends BaseAdapter {
    private ArrayList<LabelBean> mNameList;
    private LayoutInflater mInflater;
    private Context mContext;
    LinearLayout.LayoutParams params;
    private boolean isChice[];
    private int clickTemp=-1;

    // 标识选择的Item
    public void setSeclect(int position) {
        this.clickTemp = position;
    }
    public void chiceState(int post)
    {
        isChice[post]=isChice[post]==true?false:true;
        this.notifyDataSetChanged();
    }
    private void defaultSelect(List<FindDetailBean.DataBean.MatchingBean> matching){

            for(int j=0;j<matching.size();j++){
                for(int i=0;i<mNameList.size();i++){
//                    if(matching.get(j).getName().equals(mNameList.get())){
//
//                    }
                }

        }
    }
    public String getSelectList(){
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(",");
        for(int i=0;i<isChice.length;i++){
            if(isChice[i]){
                stringBuilder.append(mNameList.get(i).getId()+",");
            }
        }
        return stringBuilder.toString();
    }
    public int getSelectCount(){
        int as=0;
        for(int i=0;i<isChice.length;i++){
            if(isChice[i]){
                as++;
            }
        }
        return as;
    }
    public LabelCategaryAdapter(Context context, ArrayList<LabelBean> nameList) {
        mNameList = nameList;
        mContext = context;
        mInflater = LayoutInflater.from(context);
        isChice=new boolean[nameList.size()];
        for (int i = 0; i < nameList.size(); i++) {
            isChice[i]=false;
        }
        params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;

    }

    public int getCount() {
        return mNameList.size();
    }

    public Object getItem(int position) {
        return mNameList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        final ItemViewTag viewTag;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_gv_work_cat, null);

            // construct an item tag
            viewTag = new ItemViewTag((TextView) convertView.findViewById(R.id.tv_item));
            convertView.setTag(viewTag);
        } else {
            viewTag = (ItemViewTag) convertView.getTag();
        }
        viewTag.mName.setBackground(ContextCompat.getDrawable(mContext, R.drawable.shape_textview_common));
        if (isChice[position]== true){
            viewTag.mName.setBackground(ContextCompat.getDrawable(mContext, R.drawable.shape_textview_pressed));
        }else {
            viewTag.mName.setBackground(ContextCompat.getDrawable(mContext, R.drawable.shape_textview_common));
        }


        // set name
        viewTag.mName.setText(mNameList.get(position).getName());

        return convertView;
    }


    public void setData(ArrayList<LabelBean> nameList){
        mNameList=nameList;
        notifyDataSetChanged();
    }
    class ItemViewTag {
        protected ImageView mIcon;
        protected TextView mName;

        /**
         * The constructor to construct a navigation view tag
         *
         * @param name
         *            the name view of the item
         *            the size view of the item
         * @param icon
         *            the icon view of the item
         */
        public ItemViewTag(ImageView icon, TextView name) {
            this.mName = name;
            this.mIcon = icon;
        }
        public ItemViewTag( TextView name) {
            this.mName = name;
        }
    }
}
